/**
 * from表单提交
 * */

//form表达提交
$(".ajax-post").click(function(){
    var data,ajaxCallUrl,postUrl;

    //解决ckeditor编辑器 ajax上传问他
    if(typeof CKEDITOR=="object"){
        for(instance in CKEDITOR.instances){
            CKEDITOR.instances[instance].updateElement();
        }
    }
    d = $(this).parents('.horizontal');
    postUrl = $(this).attr('post-url');

    //按钮上的url优先
    ajaxCallUrl = postUrl ? postUrl : d.attr('action');

    layer.confirm('是否执行当前操作?', {icon: 3, title:'提示'}, function(index){
        //do something

        $.ajax({
            url : ajaxCallUrl,
            type : 'post',
            dataType : 'json',
            data : d.serialize(),
            success: function(json) {
                if(json.code == 1){

                    layer.msg(json.msg, {icon: 6})

                    if(json.data){

                        // alert(json.data);
                        if('undefined' == typeof(app)){

                        }else{


                            app.phoneLogin(json.data);
                        }
                    }
                    //  $('#alert').html(alertSuccess(json.msg));
                    //if (confirm('是否离开此页')){
                    setTimeout(function() {

                        if(json.url!='') {

                            window.location.href = json.url;
                        }
                    },800);
                    //  }

                }else if(json.code == 0){
                    layer.msg(json.msg,{icon: 6})
                    // $('#alert').html(alertDanger(json.msg));
                    /* if(json.url){
                     window.location.href=json.url;
                     }*/


                }
                setTimeout(function() {
                    $('.close').click();
                },3e3);
            },
            error:function(xhr){          //上传失败
                layer.msg(xhr.responseText)
                // $('#alert').html(alertDanger(xhr.responseText));
            }
        });


        layer.close(index);
    });

});

//a标签post提交
$('.a-post').click(function(){

    var msg =$(this).attr('post-msg');
    var url =$(this).attr('post-url');

    layer.confirm(msg, {icon: 3, title:'提示'}, function(index){


        $.ajax(
            {
                url : url,
                type : 'post',
                dataType : 'json',
                success : function (json)
                {
                    if(json.code == 1){

                        layer.msg(json.msg)
                        setTimeout(function() {
                             if(json.url){

                                 window.location.href=json.url;

                             }
                        },1000);
                    }else if(json.code == 0){
                        layer.msg(json.msg)
                    }
                    setTimeout(function() {
                        $('.close').click();
                    },3e3);
                },
                error:function(xhr){          //上传失败
                    layer.msg(xhr.responseText)
                 //   $('#alert').html(alertDanger(xhr.responseText));

                }
            });

        layer.close(index);
    });


});




function alertSuccess(data){
    return '<div class="alert alert-success" role="alert"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+data+'</div>';
}


function alertDanger(data){
    return '<div class="alert alert-danger" role="alert" style="overflow-y: auto;max-height: 600px;"><button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>'+data+'</div>';
}



function checkIdCard() {
    //获取输入身份证号码
    var ic = $("#idcard").val();
    ic = checkId(ic);

    var ic = String(ic);
    //获取性别
    var gender = ic.slice(14, 17) % 2 ? "1" : "2"; // 2代表男性，1代表女性
    set_select_checked(gender);
    var birthday = '';
    if (ic.length == 15) {
        birthday = "19" + idCard.substr(6, 6);
    } else if (ic.length == 18) {
        birthday = ic.substr(6, 8);
    }
    birthday = birthday.replace(/(.{4})(.{2})/, "$1-$2-");
    $("#input1").val(birthday);
}
function set_select_checked(checkValue) {
    var select = document.getElementById("sex");
    for (var i = 0; i < select.options.length; i++) {
        if (select.options[i].value == checkValue) {
            select.options[i].selected = true;
            break;
        }
    }
}
function checkId(pId) {
    //检查身份证号码
    var arrVerifyCode = [1, 0, "x", 9, 8, 7, 6, 5, 4, 3, 2];
    var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    var Checker = [1, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1];
    var Ai2 = '';
    var  Ai1 = '';
    if (pId.length == 0) {
        layer.msg("请先填写身份证号", {icon: 6})

    }
    if (pId.length != 15 && pId.length != 18) {
        layer.msg("身份证号共有15位或18位", {icon: 6})

    }
    var Ai = pId.length == 18 ? pId.substring(0, 17) : pId.slice(0, 6) + "19" + pId.slice(6, 16);
    if (!/^\d+$/.test(Ai))
        layer.msg("身份证除最后一位外，必须为数字！", {icon: 6})

    var yyyy = Ai.slice(6, 10), mm = Ai.slice(10, 12) - 1, dd = Ai.slice(12, 14);
    var d = new Date(yyyy, mm, dd), now = new Date();
    var year = d.getFullYear(), mon = d.getMonth(), day = d.getDate();
    if (year != yyyy || mon != mm || day != dd || d > now || year < 1800)
        layer.msg("身份证输入错误！", {icon: 6})
    for (var i = 0, ret = 0; i < 17; i++) ret += Ai.charAt(i) * Wi[i];
    var strVerifyCode = arrVerifyCode[ret %= 11];
    if(isRealNum(strVerifyCode)){
        Ai += arrVerifyCode[ret %= 11];
        Ai1 = Ai ;
        Ai2 = Ai ;
    }else{
        Ai1 =Ai + strVerifyCode.toUpperCase();
        Ai2 =Ai + strVerifyCode.toLowerCase();

    }

    if (pId.length == 18 &&( pId != Ai1&&Ai2!=pId)) {
        layer.msg("身份证输入错误！", {icon: 6})
    } else {

        return pId;
    }
};
//数字判断函数IsNumeric()
function isRealNum(val){
    // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
    if(val === "" || val ==null){
        return false;
    }
    if(!isNaN(val)){
        return true;
    }else{
        return false;
    }
}


